import { Component, Vue } from "vue-property-decorator";
import Original from "./original/index.vue";
import Diy from "./diy/index.vue";

@Component({
  components: {
    Original,
    Diy,
  },
})
export default class Home extends Vue {
  num = 1;
  //  猫咪数量
  catNumber = 0;
  // 用户所填表单内容
  form: any[] = [];
  // 计算表格
  tableData: any[] = [];
  // 天数
  day = 1;
  dayEat = 0;
  activeName = "original";
  rules = {
    age: [{ required: true, message: "请输入猫咪的年龄", trigger: "blur" }],
    weight: [{ required: true, message: "请输入猫咪的体重", trigger: "blur" }],
  };
  // 显示填表
  getTable() {
    this.catNumber = this.num;
    const number = this.catNumber;
    const form = [];
    for (let i = 0; i < number; i++) {
      const obj = {
        name: undefined,
        age: 0,
        weight: 0,
        eat: 0,
      };
      form.push(obj);
    }
    this.form = form;
  }
  // 计算数据
  getResult() {
    const form = this.form;
    let daySum = 0;
    form.map((item, index) => {
      item.num = index + 1;
      item.eat = this.getEat(item.age, item.weight);
      if (typeof item.eat == "number") {
        daySum += item.eat;
      }
    });
    this.dayEat = daySum;
    this.tableData = form;
  }
  // 判断单只食量
  getEat(age: number, weight: number) {
    let result = 0;
    if (age < 1.5) {
      result = -1;
    } else if (age < 4 && age > 1.5) {
      result = weight * 100;
    } else if (age > 4 && age < 12) {
      result = weight * 70;
    } else if (age > 12 && age < 18) {
      result = weight * 40;
    } else if (age >= 18) {
      result = weight * 30;
    }
    if (result == -1) {
      return "老老实实喂羊奶！";
    } else {
      return result;
    }
  }
}
